package com.study.www.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : 89007337.zhuyanpeng
 * @date :Create in  2019/8/14- 19:49
 **/
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(fastJsonHttpMessageConverter());
    }

    public static FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig jsonConfig = new FastJsonConfig();
        // 处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(fastMediaTypes);
        //默认的时间解析 规定默认出参格式；
        jsonConfig.setDateFormat("yyyy/MM/dd HH:mm:ss");
        //编码格式
        jsonConfig.setCharset(Charset.forName("UTF-8"));
        //时间入参格式
        JSON.DEFFAULT_DATE_FORMAT = "yyyyMMddHHmmss";
        //过滤行为
        jsonConfig.setSerializerFeatures(
                //当boolean为空时输出false
                SerializerFeature.WriteNullBooleanAsFalse,
                //若list为空则输出[]，并非null
                SerializerFeature.WriteNullListAsEmpty,
                //循环对象开启
                SerializerFeature.DisableCircularReferenceDetect
        );
        converter.setFastJsonConfig(jsonConfig);
        return converter;
    }
}
